package com.ultrasoft.farmasoft.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.User;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.service.HumanResourcesService;


@RestController
@SessionAttributes({"s_UserPharmacy"}) 
@RequestMapping("/service")
public class WebServiceLoginController {
	private final HumanResourcesService humanResourceService;

	@Autowired
    public WebServiceLoginController(HumanResourcesService humanResourceService) {
        this.humanResourceService = humanResourceService;
    }
	@RequestMapping(value="/indexServiceLogin",method = RequestMethod.POST)
	protected Userpharmacy onSubmitIndexService(@RequestParam Integer pharmacyId,@RequestParam Integer userId,HttpServletRequest request){    	
		Userpharmacy userPharmacy= humanResourceService.searchUserPharmacyByPharmacyIdByUserId(pharmacyId, userId);
		Userpharmacy temp = null;
		if(userPharmacy != null)
			temp = getUserPharmacyTemp(userPharmacy);
		return temp;
    }
	private Userpharmacy getUserPharmacyTemp(Userpharmacy userpharmacy){
		Userpharmacy userPharmacyTemp = new Userpharmacy(userpharmacy.getUserpharmacyPK().getPharmacyId(),userpharmacy.getUserpharmacyPK().getUserId(),userpharmacy.getActive());
		User userTemp = new User(userpharmacy.getUser().getUserId(),userpharmacy.getUser().getName(),userpharmacy.getUser().getLastName(),userpharmacy.getUser().getCi(),userpharmacy.getUser().getGenero(),userpharmacy.getUser().getPhone(),userpharmacy.getUser().getEmail(),userpharmacy.getUser().getLogin(),userpharmacy.getUser().getPassword());
		userPharmacyTemp.setUser(userTemp);
		Pharmacy pharmacyTemp = new Pharmacy(userpharmacy.getPharmacy().getPharmacyId(),userpharmacy.getPharmacy().getName(),userpharmacy.getPharmacy().getClient());
		userPharmacyTemp.setPharmacy(pharmacyTemp);
		return userPharmacyTemp;
	}
}
